package com.task.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.task.bean.TaskRegistBean;
import com.task.bean.TaskRunBean;
import com.task.services.TaskRegistService;
import com.task.services.TaskRunService;
import com.task.util.PageUtil;
import com.task.util.StringUtil;

@Controller
@RequestMapping(value="taskManager")
public class TaskMangerController {

	@Autowired
	private TaskRunService taskRunService;
	
	@Autowired
	private TaskRegistService taskRegistService;
	
	@RequestMapping(value="list")
	public String taskList() {
		return "order-list";
	}
	
	@RequestMapping(value="add")
	public String taskAdd(HttpServletRequest request,String id) {
		TaskRunBean bean=null;
		if(!StringUtil.isNUll(id) && !"-1".equals(id)) {
			bean=this.taskRunService.getTaskRunBean(Integer.valueOf(id));
		}else {
			bean=new TaskRunBean();
		}
		request.setAttribute("taskRegists", this.taskRegistService.getAllTaskRegist(null));
		request.setAttribute("taskRunBean", bean);
		return "order-add";
	}
	
	@ResponseBody
	@RequestMapping(value="/data")
	public Map<String,Object> getTaskData(@RequestBody Map<String,String> queryMap) throws JsonParseException, JsonMappingException, IOException{
		queryMap.putAll(PageUtil.getPage(queryMap.get("page"), queryMap.get("limit")));
		
		ObjectMapper mapper = new ObjectMapper(); 
		if(!StringUtil.isNUll(queryMap.get("search"))) {
			queryMap.putAll(mapper.readValue(queryMap.get("search"), Map.class));
			queryMap.remove("search");
		}
		
		List<TaskRunBean> registList=this.taskRunService.getAllTaskRun(queryMap);
		
		Map<String,Object>resultMap=new HashMap<String,Object>();
		resultMap.put("code", "0");
		resultMap.put("msg", "");
		resultMap.put("page", queryMap.get("page"));
		resultMap.put("limit", queryMap.get("limit"));
		resultMap.put("count", this.taskRunService.getAllTaskRunCount(queryMap));
		resultMap.put("data", registList);
		return resultMap;
	}
	
	
	@RequestMapping(value="/addTaskRunBean",method=RequestMethod.POST)
	@ResponseBody
	public  Map<String,Object> addTaskRun(@RequestBody TaskRunBean bean) {
		if(StringUtil.isNUll(bean.getId())) {
			this.taskRunService.saveTaskRun(bean);
		}else {
			this.taskRunService.updateTaskRun(bean);
		}
		return StringUtil.returnSucess();
	}
	
	@ResponseBody
	@RequestMapping(value="/del")
	public Map<String,Object> taskRundel(@RequestParam("ids") String ids) {
		this.taskRunService.delTaskRun(ids);
		 return StringUtil.returnSucess();
	}
}
